home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / TERMLIB / TGOTO.C < prev    next >
C/C++ Source or Header  |  1991-02-16  |  5KB  |  191 lines

  1. /************************************************************************
  2.  *                                      *
  3.  *              Copyright (c) 1982, Fred Fish              *
  4.  *              All Rights Reserved                  *
  5.  *                                      *
  6.  *    This software and/or documentation is released for public          *
  7.  *    distribution for personal, non-commercial use only.          *
  8.  *    Limited rights to use, modify, and redistribute are hereby      *
  9.  *    granted for non-commercial purposes, provided that all          *
  10.  *    copyright notices remain intact and all changes are clearly     *
  11.  *    documented.  The author makes no warranty of any kind with      *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular     *
  14.  *    purpose.                                  *
  15.  *                                      *
  16.  ************************************************************************
  17.  */
  18. /*
  19.  * Modified:
  20.  *    1 May 86 ...!ihnp4!ut-sally!ut-ngp!mic
  21.  *        Now forces a '\0' at end of tgoto string.  Tgoto wasn't,
  22.  *        and this screwed up VT100-style (i.e. variable) cursor
  23.  *        addressing.
  24.  *
  25.  *    22 Jan 89 blarson@skat.usc.edu
  26.  *        Fix %. so it won't generate '\0'.  This requires UP
  27.  *        and BC externals.  Avoid troucing memory if bad format
  28.  *        string given.  Make smaller and faster.  Some comments
  29.  *        on the obvious eliminated to make code easier to read.
  30.  */
  31.  
  32.  
  33.  
  34. /*
  35.  *  LIBRARY FUNCTION
  36.  *
  37.  *    tgoto   expand cursor addressing string from cm capability
  38.  *
  39.  *  KEY WORDS
  40.  *
  41.  *    termcap
  42.  *
  43.  *  SYNOPSIS
  44.  *
  45.  *    extern char *UP;
  46.  *    extern char *BC;
  47.  *
  48.  *    char *tgoto(cm,destcol,destline)
  49.  *    char *cm;
  50.  *    int destcol;
  51.  *    int destline;
  52.  *
  53.  *  DESCRIPTION
  54.  *
  55.  *    Returns cursor addressing string, decoded from the cm
  56.  *    capability string, to move cursor to column destcol on
  57.  *    line destline.
  58.  *
  59.  *    The following sequences uses one input argument, either
  60.  *    line or column, and place the appropriate substitution
  61.  *    in the output string:
  62.  *
  63.  *          %d      substitute decimal value (in ASCII)
  64.  *          %2      like %d but forces field width to 2
  65.  *          %3      like %d but forces field width to 3
  66.  *          %.      like %c
  67.  *          %+x     like %c but adds ASCII value of x
  68.  *
  69.  *    The following sequences cause processing modifications
  70.  *    but do not "use up" one of the arguments.    If they
  71.  *    act on an argument they act on the next one to
  72.  *    be converted.
  73.  *
  74.  *          %>xy    if next value to be converted is
  75.  *              greater than value of ASCII char x
  76.  *              then add value of ASCII char y.
  77.  *          %r      reverse substitution of line
  78.  *              and column (line is substituted
  79.  *              first by default).
  80.  *          %i      causes input values destcol and
  81.  *              destline to be incremented.
  82.  *          %%      gives single % character in output.
  83.  *
  84.  *  BUGS
  85.  *
  86.  *    Does not implement some of the more arcane sequences for
  87.  *    radically weird terminals (specifically %n, %B, & %D).
  88.  *    If you have one of these you deserve whatever happens.
  89.  *
  90.  */
  91.  
  92. #include <stdio.h>
  93.  
  94. extern char *UP;        /* sequence to goto previous line */
  95. extern char *BC;        /* sequence to goto previous char */
  96.  
  97. char *tgoto(cm,destcol,destline)
  98. char *cm;
  99. int destcol;
  100. int destline;
  101. {
  102.     register char *in;        /* Internal copy of input string pointer */
  103.     register char *out;        /* Pointer to output array */
  104.     register int pcount;    /* Count of args processed */
  105.     int args[2];        /* args to convert */
  106.     static char output[64];    /* Converted string */
  107.     int repos;            /* flags that UP and BC are needed */
  108.                 /* 1 for UP, 2 for BC, 3 for both */
  109.  
  110.     if (cm == NULL) return "OOPS";
  111.     in = cm;
  112.     out = output;
  113.     args[0] = destline;
  114.     args[1] = destcol;
  115.     pcount = 0;
  116.     repos = 0;
  117.     while (*in != '\0') {
  118.     if (*in != '%') {
  119.         *out++ = *in++;
  120.     } else {
  121.         in++;
  122.         switch(*in++) {
  123.         case 'd':
  124.             sprintf(out,"%d",args[pcount]);
  125.             out = &output[strlen(output)];
  126.             pcount ^= 1;
  127.             break;
  128.         case '2':
  129.             sprintf(out,"%02d",args[pcount]);
  130.             out = &output[strlen(output)];
  131.             pcount ^= 1;
  132.             break;
  133.         case '3':
  134.             sprintf(out,"%03d",args[pcount]);
  135.             out = &output[strlen(output)];
  136.             pcount ^= 1;
  137.             break;
  138.         case '.':
  139.             if((*out++ = args[pcount]) == '\0') {
  140.                 out[-1]++;
  141.             repos |= pcount+1;
  142.             }
  143.             pcount ^= 1;
  144.             break;
  145.         case '+':
  146.             if((*out++ = args[pcount] + *in++) == '\0') {
  147.                   out[-1]++;
  148.              repos |= pcount+1;
  149.             }
  150.             pcount ^= 1;
  151.             break;
  152.         case '>':
  153.             if (args[pcount] > *in++) {
  154.             args[pcount] += *in++;
  155.             } else {
  156.             in++;
  157.             }
  158.             break;
  159.         case 'r':
  160.             pcount ^= 1;
  161.             break;
  162.         case 'i':
  163.             args[0]++;
  164.             args[1]++;
  165.             break;
  166.         case '%':
  167.             *out++ = '%';
  168.             break;
  169.         }
  170.     }
  171.     }
  172.     if(repos) {
  173.     if(repos & 1) {
  174.         if(UP != NULL) {
  175.         in = UP;
  176.           while(*in) *out++ = *in++;
  177.         }    /* else the output is screwed up */
  178.     }
  179.     if(repos & 2) {
  180.         if(BC != NULL && *BC != '\0') {
  181.           in = BC;
  182.             while(*in) *out++ = *in++;
  183.         } else {
  184.             *out++ = '\b';
  185.         }
  186.     }
  187.     }
  188.     *out = '\0';
  189.     return output;
  190. }
  191.